﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BullTasDesaDal.Service;
using System.Data;
using BullTasDesaDal.Entities;
using System.Reflection;
using AjaxControlToolkit;

namespace WSBulTasDesa
{
    public partial class wfAppraisalList : System.Web.UI.Page
    {
        private IList<Appraisal> lappraisal
        {
            get
            {
                if (Session["appraisal"] != null)
                    return (IList<Appraisal>)Session["appraisal"];
                else
                {
                    lappraisal = new List<Appraisal>();
                    return lappraisal;
                }
            }
            set { Session["appraisal"] = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {


        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {

            lappraisal = AppraisalService.GetAll(ucFilter.Filter);
            LoadGrid();
        }

        protected void LoadGrid()
        {
            grvAppraisal.DataSource = lappraisal;
            grvAppraisal.DataBind();
        }

        protected void grvAppraisal_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int idAppraisal = 0;
            switch (e.CommandName)
            {
                case "Edit":
                    idAppraisal = Convert.ToInt32(grvAppraisal.DataKeys[Convert.ToInt16(e.CommandArgument)].Value.ToString());
                    Response.Redirect("wfAppraisal.aspx?idApprasisal=" + idAppraisal.ToString());
                    break;
                case "View":
                    idAppraisal = Convert.ToInt32(grvAppraisal.DataKeys[Convert.ToInt16(e.CommandArgument)].Value.ToString());
                    ucDetails.Appraisal = lappraisal.Where(x => x.Id == idAppraisal).Single();
                    extDetails.Show();
                    break;
                case "Land":
                    idAppraisal = Convert.ToInt32(grvAppraisal.DataKeys[Convert.ToInt16(e.CommandArgument)].Value.ToString());
                    Response.Redirect("wfLandAnalysis.aspx?idAppraisal=" + idAppraisal.ToString());
                    break;
                case "Report":
                    idAppraisal = Convert.ToInt32(grvAppraisal.DataKeys[Convert.ToInt16(e.CommandArgument)].Value.ToString());
                    Response.Redirect("wfReport.aspx?idAppraisal=" + idAppraisal.ToString());
                    break;
                default:
                    break;
            }
        }

        protected void grvAppraisal_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grvAppraisal.PageIndex = e.NewPageIndex;
            LoadGrid();
        }

        protected void grvAppraisal_Sorting(object sender, GridViewSortEventArgs e)
        {

            if (e.SortExpression == (string)ViewState["SortColumn"])
                e.SortDirection = ((SortDirection)ViewState["SortColumnDirection"] == SortDirection.Ascending) ? SortDirection.Descending : SortDirection.Ascending;

            ViewState["SortColumn"] = e.SortExpression;
            ViewState["SortColumnDirection"] = e.SortDirection;

            PropertyInfo prop = typeof(Appraisal).GetProperty(e.SortExpression);

            switch (e.SortDirection)
            {
                case SortDirection.Ascending:
                    lappraisal = lappraisal.OrderBy(x => prop.GetValue(x, null)).ToList();
                    break;
                case SortDirection.Descending:
                    lappraisal = lappraisal.OrderByDescending(x => prop.GetValue(x, null)).ToList();
                    break;
                default:
                    break;
            }

            //grvAppraisal.Columns[4].ItemStyle.BackColor = System.Drawing.Color.Red;
            //grvAppraisal.SortedAscendingCellStyle
            LoadGrid();




        }
    }



}